from django.contrib.auth import authenticate
from rest_framework import serializers
from django.utils.translation import ugettext as _
from rest_framework_jwt.settings import api_settings
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

from rest_framework_jwt.serializers import JSONWebTokenSerializer
from rest_framework_jwt.views import JSONWebTokenAPIView


class AdminJSONWebTokenSerializer(JSONWebTokenSerializer):
    def validate(self, attrs):
        credentials = {
            self.username_field: attrs.get(self.username_field),
            'password': attrs.get('password')
        }

        if all(credentials.values()):
            user = authenticate(**credentials)

            if user:
                # 判断用户是否激活
                if not user.is_active:
                    msg = _('User account is disabled.')
                    raise serializers.ValidationError(msg)

                # 判断用户是否是管理员
                if not user.is_staff:
                    # 如不是,则返回没有权限
                    raise serializers.ValidationError('您没有权限登录')

                payload = jwt_payload_handler(user)

                return {
                    'token': jwt_encode_handler(payload),
                    'user': user
                }
            else:
                msg = _('Unable to log in with provided credentials.')
                raise serializers.ValidationError(msg)
        else:
            msg = _('Must include "{username_field}" and "password".')
            msg = msg.format(username_field=self.username_field)
            raise serializers.ValidationError(msg)


# class AdminJSONWebTokenSerializer(JSONWebTokenSerializer):
#     def validate(self, attrs):
#         credentials = {
#             self.username_field: attrs.get(self.username_field),
#             'password': attrs.get('password')
#         }
#         if all(credentials.values()):
#             user = authenticate(**credentials)
#
#             if user:
#                 # 判断用户是否激活
#                 if not user.is_active:
#                     msg = _('User account is disabled.')
#                     raise serializers.ValidationError(msg)
#                 # 判断用户是否是管理员
#                 if not user.is_staff:
#                     # 如不是,则返回没有权限
#                     raise serializers.ValidationError('您没有权限登录')
#
#                 payload = jwt_payload_handler(user)
#
#                 return {
#                     'token': jwt_encode_handler(payload),
#                     'user': user
#                 }
#             else:
#                 msg = _('Unable to log in with provided credentials.')
#                 raise serializers.ValidationError(msg)
#         else:
#             msg = _('Must include "{username_field}" and "password".')
#             msg = msg.format(username_field=self.username_field)
#             raise serializers.ValidationError(msg)


# 使用自己的序列化器
class AdminObtainJSONWebToken(JSONWebTokenAPIView):
    """
    API View that receives a POST with a user's username and password.
    Returns a JSON Web Token that can be used for authenticated requests.
    """
    serializer_class = AdminJSONWebTokenSerializer

admin_obtain_jwt_token=AdminObtainJSONWebToken.as_view()


# 使用自定义序列化器
# class AdminObtainJSONWebToken(JSONWebTokenAPIView):
#
#     serializers_class =  AdminJSONWebTokenSerializer
#
# admin_obtain_jwt_token = AdminJSONWebTokenSerializer
